https://leetcode.cn/problems/intersection-of-two-linked-lists/solutions/811625/xiang-jiao-lian-biao-by-leetcode-solutio-a8jn/?envType=study-plan-v2&envId=top-100-liked

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
//这种更好的双指针，快慢指针，让长的先走差值步，然后快慢指针一起走
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode* curA = headA,*curB = headB;
    int cotA = 1, cotB = 1;
    while(curA->next)
    {
        curA = curA->next;
        cotA++;
    }
    while(curB->next)
    {
        curB = curB->next;
        cotB++;
    }
    if(curA != curB)
    {
        return NULL;
    }
    struct ListNode* longlist = headA,*shortlist = headB;
    int gap = abs(cotA - cotB);
    if(cotB > cotA)
    {
        shortlist = headA;
        longlist = headB;
    }
    while(gap--)
    {
        longlist = longlist->next;
    }
    while(longlist != shortlist)
    {
        longlist = longlist->next;
        shortlist = shortlist->next;
    }
    return longlist;
}

//双指针
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 typedef struct ListNode* pListNode;
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(headA==NULL || headB == NULL)
        return NULL;
    pListNode pa = headA, pb = headB;
    while(pa != pb)
    {
        if(pa == NULL) pa = headB;
        else pa = pa->next;

        if(pb == NULL) pb = headA;
        else pb = pb->next;
    }
    return pa;
}